/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.dao.CashcustodianreportChecksDAO;
import com.cashmate.hibernate.dao.CashcustodianreportDAO;
import com.cashmate.hibernate.dao.CheckDAO;
import com.cashmate.hibernate.dao.CollectionsDAO;
import com.cashmate.hibernate.pojo.Additionalfund;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Bankcreditmemo;
import com.cashmate.hibernate.pojo.Bankdebitmemo;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Cashcustodianreport;
import com.cashmate.hibernate.pojo.CashcustodianreportChecks;
import com.cashmate.hibernate.pojo.CashcustodianreportCollections;
import com.cashmate.hibernate.pojo.CashcustodianreportDepositintransit;
import com.cashmate.hibernate.pojo.Cashdisbursement;
import com.cashmate.hibernate.pojo.Checks;
import com.cashmate.hibernate.pojo.Collections;
import com.cashmate.hibernate.pojo.Employees;
import com.opensymphony.xwork2.ActionSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;

import java.util.List;
import java.util.Set;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Sarah
 */
public class ViewCCR extends ActionSupport {

    private AreaDAO ar = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private CollectionsDAO cd = (CollectionsDAO) ServletActionContext.getServletContext().getAttribute("collectionsDAO");
    private BranchDAO bd = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private CheckDAO checkdao = (CheckDAO) ServletActionContext.getServletContext().getAttribute("checksDAO");
    private CashcustodianreportDAO ccrdao = (CashcustodianreportDAO) ServletActionContext.getServletContext().getAttribute("cashcustodianreportDAO");
    private CashcustodianreportChecksDAO ccrcheckdao = (CashcustodianreportChecksDAO) ServletActionContext.getServletContext().getAttribute("cashcustodianreportchecksDAO");
    private String loc;
    private String view;
    private String date;
    private int branch;
    private List<Collections> colldeposit;
    private List<Collections> collection;
    private List<Checks> checklist;
    private Cashcustodianreport latestccr;
    private String bankcode;
    private String acctnum;

    public String getView() {
        return view;
    }

    public void setView(String view) {
        this.view = view;
    }

    public void setAcctnum(String acctnum) {
        this.acctnum = acctnum;
    }

    public void setBankcode(String bankcode) {
        this.bankcode = bankcode;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setBranch(int branch) {
        this.branch = branch;
    }

    public String getLoc() {
        return loc;
    }

    public List<Collections> getColldeposit() {
        return colldeposit;
    }

    public List<Collections> getCollection() {
        return collection;
    }

    public List<Checks> getChecklist() {
        return checklist;
    }

    public CashcustodianreportDAO getCcrdao() {
        return ccrdao;
    }

    @Action(value = "/view-ccr", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {
        try {
            loc = "../../ccr.jsp";
            Branches b = bd.get(branch);

            Cashcustodianreport ccr = ccrdao.getByBranchAndDate(b, date);

            if (ccr == null) {      //NO SAVED CCR FOR THE DATE AND AREA SPECIFIED
                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                Date curdate = df.parse(date);

                Calendar cal = Calendar.getInstance();
                cal.setTime(curdate);
                cal.add(Calendar.DATE, -1);
                String dateyesterday = df.format(cal.getTime());
                System.out.println("dateyest:" + df.format(cal.getTime()));

                Branches branchccr = bd.get(branch);
                List<Areas> arealist = ar.getAreasPerBranch(branch);
                colldeposit = cd.getCCRCollection(dateyesterday, arealist);     //Deposit in Transit (fields from collection dated yesterday)
                collection = cd.getCCRCollection(date, arealist);               //Collection (fields from collection dated today)
                checklist = checkdao.getCCRChecks(date, branch);
                latestccr = ccrdao.getCCREndBal();
                System.out.println(latestccr);

                ServletActionContext.getRequest().setAttribute("checklist", checklist);
                ServletActionContext.getRequest().setAttribute("collection", collection);
                ServletActionContext.getRequest().setAttribute("colldeposit", colldeposit);
                ServletActionContext.getRequest().setAttribute("bankcode", bankcode);
                ServletActionContext.getRequest().setAttribute("acctnum", acctnum);
                ServletActionContext.getRequest().setAttribute("date", date);
                ServletActionContext.getRequest().setAttribute("branchccr", branchccr);
                ServletActionContext.getRequest().setAttribute("latestccr", latestccr);
                view = "yes";
            } else {
                loc = "../../ccr.jsp?cat=savedccr";

                Employees employees = ccr.getEmployees();
                Branches branches = ccr.getBranches();
                int ccrid = ccr.getCcrid();
                List<CashcustodianreportChecks> ccrchecklist = ccrcheckdao.getChecksPerCCR(ccr);
                List<CashcustodianreportCollections> ccrcoll = ccrdao.getCollPerCCR(ccrid);
                List<CashcustodianreportDepositintransit> deplist = ccrdao.getDepositPerCCR(ccrid);

                List<Checks> checks = new ArrayList<Checks>();
                List<Collections> collectionlist = new ArrayList<Collections>();
                List<Collections> depositlist = new ArrayList<Collections>();

                for (int i = 0; i < ccrchecklist.size(); i++) {
                    checks.add(ccrchecklist.get(i).getChecks());
                }

                for (int i = 0; i < ccrcoll.size(); i++) {
                    collectionlist.add(cd.get(ccrcoll.get(i).getCollectionid()));
                }

                for (int i = 0; i < deplist.size(); i++) {
                    depositlist.add(cd.get(deplist.get(i).getCollectionid()));
                }

                Set<Additionalfund> addtlfundlist = ccr.getAdditionalfundes();
                Set<Cashdisbursement> cashlist = ccr.getCashdisbursementes();
                Set<Bankdebitmemo> bankdebitlist = ccr.getBankdebitmemoes();
                Set<Bankcreditmemo> bankcreditlist = ccr.getBankcreditmemoes();

                ServletActionContext.getRequest().setAttribute("employees", employees);
                ServletActionContext.getRequest().setAttribute("branches", branches);
                ServletActionContext.getRequest().setAttribute("cashcustodian", ccr);
                ServletActionContext.getRequest().setAttribute("checklist", checks);
                ServletActionContext.getRequest().setAttribute("cashlist", cashlist);
                ServletActionContext.getRequest().setAttribute("addtlfundlist", addtlfundlist);
                ServletActionContext.getRequest().setAttribute("bankdebitlist", bankdebitlist);
                ServletActionContext.getRequest().setAttribute("bankcreditlist", bankcreditlist);
                ServletActionContext.getRequest().setAttribute("collectionlist", collectionlist);
                ServletActionContext.getRequest().setAttribute("depositlist", depositlist);
            }
            return SUCCESS;

        } catch (Exception e) {
            System.out.println("viewccrerr:" + e.toString());
            e.printStackTrace();
        }
        return SUCCESS;
    }
}
